﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XmlStudio.Interfaces;

namespace XmlStudio.Models.Actions {
    internal sealed class SetDocumentTypeAction : XmlModelAction {
        #region Fields
        #region Private
        private string oldName;
        private string oldPublicId;
        private string oldSystemId;
        private string oldInternalSubset;

        private string name;
        private string publicId;
        private string systemId;
        private string internalSubset;
        #endregion
        #endregion

        #region Constructors
        public SetDocumentTypeAction(IXmlModelWithNotifications model, string name, string publicId, string systemId, string internalSubset)
            : base(model) {
            //if(string.IsNullOrEmpty(name)) {
            //    throw new ArgumentNullException("name");
            //}

            this.name = name;
            this.publicId = publicId;
            this.systemId = systemId;
            this.internalSubset = internalSubset;

            this.oldName = model.DocumentType != null ? model.DocumentType.Name : null;
            this.oldPublicId = model.DocumentType != null ? model.DocumentType.PublicId : null;
            this.oldSystemId = model.DocumentType != null ? model.DocumentType.SystemId : null;
            this.oldInternalSubset = model.DocumentType != null ? model.DocumentType.InternalSubset : null;
        }
        #endregion

        #region Methods
        protected override void ExecuteCore() {
            this.model.DocumentType = this.model.XmlDocument.CreateDocumentType(this.name, this.publicId, this.systemId, this.internalSubset);
            this.model.OnRawXmlChanged(new Events.RawXmlChangedEventArgs());
        }

        protected override void UnExecuteCore() {
            if(string.IsNullOrEmpty(this.oldName)) {
                this.model.DocumentType = null;
                this.model.OnRawXmlChanged(new Events.RawXmlChangedEventArgs());
            } else {
                this.model.DocumentType = this.model.XmlDocument.CreateDocumentType(this.oldName, this.oldPublicId, this.oldSystemId, this.oldInternalSubset);
                this.model.OnRawXmlChanged(new Events.RawXmlChangedEventArgs());
            }
        }
        #endregion
    }
}
